﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataAccessLayer;

namespace PresentationLayerMVC.Controllers
{
    [Authorize(Roles="Administrator")]
    public class FacilityController : Controller
    {
        //
        // GET: /Facility/
        FacilityRepository facilityRepository = new FacilityRepository();

        public ActionResult Index()
        {
            IEnumerable<Facility> facilities = facilityRepository.GetAll();
            return View(facilities);
        }

        //
        // GET: /Facility/Details/5

        public ActionResult Details(int id)
        {
            Facility facility = facilityRepository.FindById(id);
            return View(facility);
        }

        //
        // GET: /Facility/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Facility/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                Facility facility = new Facility();
                UpdateModel(facility);
                facilityRepository.Add(facility);
                facilityRepository.Commit();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Facility/Edit/5
 
        public ActionResult Edit(int id)
        {
            Facility facility = facilityRepository.FindById(id);
            return View(facility);
        }

        //
        // POST: /Facility/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                Facility facility = facilityRepository.FindById(id);
                UpdateModel(facility);
                facilityRepository.Commit();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Facility/Delete/5
 
        public ActionResult Delete(int id)
        {
            Facility facility = facilityRepository.FindById(id);
            facilityRepository.Delete(facility);
            facilityRepository.Commit();

            return View("Index");
        }

        //
        // POST: /Facility/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
